package com.wang.mandkey.net;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JTextArea;

import com.wang.mandkey.tool.Tool;
import com.wang.mandkey.utils.ProjectEnvironment;

/**
 * 
 * @author wang 下午04:18:53-2011 TODO 提供屏幕图像发送线程
 */
public class ScreenProviderThread implements Runnable {

	private ServerSocket mServerSocket = null;
	private Socket mSocketClient = null;
	private InputStream mInputStream = null;
	private OutputStream mOutputStream = null;
	private boolean isBlock = false;
	private Thread thread = null;
	private JTextArea jta;

	public ScreenProviderThread(JTextArea jta) {
		try {
			mServerSocket = new ServerSocket(ProjectEnvironment.INT_DEFAULT_PORT_IMG);
			ProjectEnvironment.BOOLEAN_STOP_SEND_IMG = false;
			this.jta = jta;
			thread = new Thread(this);
			thread.start();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {

		while (true) {// 这个循环是等待客户端连接
			try {
				mSocketClient = mServerSocket.accept();
				mSocketClient.setSendBufferSize(512);
				ProjectEnvironment.BOOLEAN_STOP_SEND_IMG = false;
				System.out.println("发送截图线程开始");
				jta.append("发送截图线程开始\n");
				isBlock = false;
			} catch (IOException e1) {
				e1.printStackTrace();
				break;
			}

			try {
				this.mOutputStream = mSocketClient.getOutputStream();
				this.mInputStream = new DataInputStream(mSocketClient.getInputStream());
				
				 String phoneInfo = "";
				 byte[] bufinfo=new byte[8192];
			     mInputStream.read(bufinfo);
			     phoneInfo=new String(bufinfo,0,bufinfo.length); 
				
				if (null != phoneInfo&& phoneInfo.startsWith(ProjectEnvironment.YOUR_PHONE_DIMEN)) {
					int[] dimen = Tool.parserPhoneDimen(phoneInfo);
					ProjectEnvironment.PHONE_H = dimen[0];
					ProjectEnvironment.PHONE_W = dimen[1];
//					System.err.println(ProjectEnvironment.PHONE_H+"   "+ProjectEnvironment.PHONE_W );
				}
				if (null != this.mOutputStream) {//
					int pcount = 0;
					while (!isBlock) { // 发送每帧图像的循环
					    byte[] datas = Tool.getNowScreen2();
					    jta.append("发送一帧图片开始:length:"+datas.length+"\n");
						pcount++;
						if(!isBlock){
							this.mOutputStream.write(datas, 0, datas.length);
							this.mOutputStream.write("19871104".getBytes());
							this.mOutputStream.flush();
						}
						// 判断是否停止发送图片
						if (ProjectEnvironment.BOOLEAN_STOP_SEND_IMG) {
							this.mOutputStream.write(ProjectEnvironment.BYTE_ENDDATA);
							break;
						}
						
						System.out.println("发送第" + pcount + "张图片!");
						jta.append("发送第" + pcount + "张图片!\n");
					}
				}

			} catch (IOException e) {
				e.printStackTrace();
				isBlock = true;
			}

		}
	}

	public void killSelf() {
		if (null != mServerSocket) {
			try {
				mServerSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		ProjectEnvironment.BOOLEAN_STOP_SEND_IMG = true;
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
		if (null != mServerSocket) {
			mServerSocket.close();
		}
	}

}
